package com.example.practice;


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentDie2 extends Fragment {
    private View view;
    private LineChart mLineChar;
    private String s="";
    public FragmentDie2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_die2, container, false);
        initView();
        initData();
        return view;
    }

    private void initData() {
        List<String> lists=new ArrayList<>();
        ArrayList<Entry> values = new ArrayList<>();
        int i=0;
        SQLiteDatabase db = getActivity().openOrCreateDatabase("info", getActivity().MODE_PRIVATE, null);
        Cursor info1 = db.query("weather", null, null,null, null, null, null);
        if(info1!= null){
            while(info1.moveToNext()) {
                lists.add(info1.getString(info1.getColumnIndex("weather7")));
                s="湿度";
                i++;
                values.add(new Entry(i,info1.getInt(info1.getColumnIndex("weather2"))));
            }
        }

        LineDataSet set1 = new LineDataSet(values, s);
        set1.setDrawValues(true);// 在坐标点上显示 y 数值
        set1.setFormSize(0);
        set1.setDrawCircles(false);set1.setDrawCircleHole(false); // 不显示相应坐标点的小圆圈
        set1.setLineWidth(2f); // 设置线的宽度
        set1.getColor(R.color.color_L);// 设置线的颜色
        XAxis xAxis = mLineChar.getXAxis();
        xAxis.setDrawGridLines(false);// 取消网格线显示
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 位置改到图表下方
        xAxis.setAxisMinimum(0f);// x轴起始改为0
        xAxis.setGranularity(1f);// x轴的最小单位设置为1f
        set1.setValues(values);
        // 改变x轴的显示内容，
        // 然后设置valueFormatter将x轴的数值对应上日期，然后实际上就会显示日期了。
//        xAxis.setValueFormatter(new IAxisValueFormatter() {
//            @Override
//            public String getFormattedValue(float value, AxisBase axis) {
//                return lists.get((int) value);
//            }
//        });
        YAxis axisRight = mLineChar.getAxisRight();
        axisRight.setEnabled(false);
        YAxis axisLeft = mLineChar.getAxisLeft();
        axisLeft.setAxisMinimum(0f);
        Legend legend = mLineChar.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER );
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP );
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); //图例 水平摆放
        legend.setTextColor(Color.parseColor("#7e7e7e")) ; //设置图例标签的颜色。
        legend.setTextSize(90f);  //设置图例标签的大小 单位dp
        legend.setFormToTextSpace(5f); //设置 legend-form 和 legend-label 之间的空间。单位dp。
        legend.setFormSize(10f);  //设置 legend-forms 的大小，单位dp。
        legend.setDrawInside(false); //图例会知道chart的外面
        LineData data = new LineData(set1);
        mLineChar.setData(data);
        mLineChar.moveViewToX(data.getXMax());
    }

    private void initView() {
        mLineChar = view.findViewById(R.id.lineChart2);

    }

}
